//
//  ZYXChangeNameViewController.m
//  RuiBeiKang
//
//  Created by 代码女神 on 2019/1/11.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXChangeNameViewController.h"

@interface ZYXChangeNameViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameTextF;

@end

@implementation ZYXChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改昵称";
    self.view.backgroundColor = [UIColor whiteColor];
    self.nameTextF.delegate = self;
}

- (IBAction)saveAction:(id)sender {
    [self.view endEditing:YES];
    NSString *name = self.nameTextF.text;
    name = [name  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (name.length<1) {
        [JKToast showWithText:@"请输入昵称"];
        return;
    }else{
        if ([Functions isNickName:name]){
             [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDUserNameUpdat) parmas:@{@"user_id":UID,@"nickname":name} successBloock:^(NSDictionary *responseObject) {
                 NSLog(@"%@",responseObject);
                 if ([responseObject[@"code"] isEqualToNumber:@200]) {
                     [self.delegate didSaveUserName:name];
                     [self.navigationController popViewControllerAnimated:YES];
                     
                 }else{
                     [JKToast showWithText:responseObject[@"message"]];
                 }
                 
             } failureBlock:^(NSError *error) {
                 
             }];
         }else{
             [JKToast showWithText:@"昵称长度不能超过八位数"];
         }
        
    }
    

}
//用户名
- (BOOL)CrazyValidateName
{
    NSString *userNameRegex = @"^[A-Za-z0-9_\u4e00-\u9fa5]{4,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:self];
    return B;
}
@end
